package com.hsmpay.mobile.action.user;

import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.NumberUtil;
import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.pojo.user.UserMoney;
import com.hsmpay.mobile.service.user.UserMoneyService;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户虚拟货币action
 *
 * @author 颜铃璋
 * @date 2013-1-28
 */
@Controller("userMoneyAction")
@Scope("prototype")
public class UserMoneyAction extends MobileBaseAction {
    private static final long serialVersionUID = -5850367982749551414L;
    private static Logger log = Logger.getLogger(UserMoneyAction.class);

    @Resource(name = "userMoneyService")
    private UserMoneyService<UserMoney, Long> userMoneyService;
//	@Resource(name="accountDetailsService")
//	private AccountDetailsService<AccountDetails,Long> accountDetailsService;

    /**
     * 查询用户虚拟货币
     *
     * @return
     * @throws Exception
     */
    public String searchUserMoney() throws Exception {
        log.debug("--- searchUserMoney() start ---");
        userId = Long.parseLong(getJsonString("userId"));

        log.debug("userId = " + userId);
        if (null == userId) {
            log.warn("查询用户虚拟货币时, 传入参数userId为空");
            sendAppErrorMsg("6001", "userId");
            return null;
        }
        try {
            User user = userService.getUserById(userId);
            if (null != user) {
                //查询用户虚拟货币
                UserMoney userMoney = new UserMoney();
                userMoney.setUserId(userId);
                userMoney = userMoneyService.searchEntity(userMoney);

                Map<String, Object> result = new HashMap<String, Object>();//返回结果
                if (null != userMoney) {
                    log.debug(userMoney.getMoney() + "    " + userMoney.getMoney());
                    Double money = NumberUtil.roundF2(userMoney.getMoney() + userMoney.getAvailMoney());
                    result.put("money", NumberUtil.numFormat(money, "0.00"));
                    result.put("availMoney", NumberUtil.numFormat(userMoney.getAvailMoney(), "0.00"));
                    //result.put("freezeMoney",NumberUtil.numFormat(userMoney.getFreezeMoney(), "0.00"));
                } else {
                    result.put("money", "0.00");
                    result.put("availMoney", "0.00");
                    //result.put("freezeMoney","0.00");
                }

                result.put("reqCode", "0000");
                result.put("reqMsg", "查询用户虚拟货币成功！");
                sendAppMsg(result);//返回结果给手机端
            } else {
                log.warn("不存在用户userId=" + userId + " 的用户！");
                sendAppErrorMsg("6012", userId + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.debug("--- searchUserMoney() end ---");
        }
        return null;
    }

//	/**
//	 * 收支明细
//	 * Income And Expenditure Details
//	 * @return
//	 */
//	@Deprecated
//	public String incAndExpDetailsList()throws Exception{
//		log.debug("--- IncAndExpDetailsList() start ---");
//		log.debug("userId = "+ userId);
//		if(null == userId){
//			log.warn("查询收支明细列表时, 传入参数userId为空");
//			sendAjaxResponse("0");
//			return null;
//		}
//		if(null == start){
//			start = 0;
//		}
//		if(null == stop){
//			stop = 20;
//		}
//		try{
//			//查询用户虚拟货币
//			AccountDetails param = new AccountDetails();
//			param.setUserId(userId);
//			List<AccountDetails> accountDetailsList = accountDetailsService.searchEntityList(param);
//			List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();//返回结果
//			Map<String,Object> element = null;
//			for(AccountDetails accountDetails : accountDetailsList){
//				element = new HashMap<String, Object>();
//				element.put("orderNum", accountDetails.getOrderNum());
//				element.put("category", accountDetails.getOrderNum());
//				element.put("incExp", accountDetails.getIncExp());
//				element.put("transMoney", accountDetails.getTransMoney());
//				element.put("balance", accountDetails.getBalance());
//				element.put("status", accountDetails.getStatue());
//				result.add(element);
//			}
//			sendAjaxResponse(result);//返回结果给手机端
//		}catch(Exception e){
//			e.printStackTrace();
//			throw e ;
//		}finally{
//			log.debug("--- IncAndExpDetailsList() end ---");
//		}
//		return null;
//	}

    //---------------------set get start--------------------
    //---------------------set get end----------------------
}
